package com.asura.app.wechat.ui.moment;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.asura.app.wechat.R;
import com.asura.app.wechat.utils.UIUtils;

/**
 * Created by Asura on 2017/12/29.
 */

public class TweetListDividerItemDecoration extends RecyclerView.ItemDecoration {

    private final int dividerHeight;
    private final Paint paint;


    public TweetListDividerItemDecoration(Context context) {
        dividerHeight = UIUtils.dip2px(context, 0.5f);
        paint = new Paint();
        paint.setColor(ContextCompat.getColor(context, R.color.divider));
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int pos = parent.getChildAdapterPosition(view);
        if (pos > 0) {
            outRect.set(0, 0, 0, dividerHeight);
        } else {
            outRect.set(0, 0, 0, 0);
        }
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();
        c.save();
        int count = parent.getChildCount();
        for (int i = 0; i < count; i++) {
            View child = parent.getChildAt(i);
            int pos = parent.getChildAdapterPosition(child);
            if (pos > 0) {
                RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
                int top = child.getBottom() + layoutParams.bottomMargin;
                int bottom = top + dividerHeight;
                c.drawRect(left, top, right, bottom, paint);
            }
        }
        c.restore();
    }
}
